public class solution {
    /*
     * 二叉树的最小深度
     * DFS
     * */
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDep = minDepth(root.left);
        int rightDep = minDepth(root.right);
        if (leftDep == 0) {
            return rightDep + 1;
        }
        if (rightDep == 0) {
            return leftDep + 1;
        }
        return (leftDep > rightDep ? rightDep : leftDep) + 1;
    }

    /*
     * BFS
     *
     * */
    public static int minDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        int minDepth = 1;
        queue.offer(root);
        while (true) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null) {
                    return minDepth;
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            minDepth++;
        }
    }
}